package jjn.carl.dp;

/**
 * @author Jiang Jining
 * @since 2023-08-13 15:21
 */
public class LeetCode416_2 {
    public boolean canPartition(int[] nums) {
        int sum = 0;
        int maxNum = nums[0];
        for (int num : nums) {
            sum += num;
            maxNum = Math.max(maxNum, num);
        }
        if (sum % 2 == 1) {
            return false;
        }
        int target = sum / 2;
        if (maxNum > target) {
            return false;
        }
        boolean[] dp = new boolean[target + 1];
        dp[0] = true;
        for (int i = 0; i < nums.length; i++) {
            for (int j = target; j >= nums[i]; j--) {
                dp[j] = dp[i - nums[j]] || dp[j];
            }
        }
        return dp[target];
    }
}
